import { getuser } from '../../utils/storage'
import { request, wxlogin, request2 } from '../../utils/https'
import {
  getschoolstudenthomeworkbyid,
  changeschoolhomeworkitembankreply
} from '../../utils/interfaces'
import { values, sortBy } from 'underscore'
export default {
  name: 'assignment',
  data () {
    return {
      pj: true,
      visible1: false,
      visible2: false,
      homework: {
        ID: '',
        createdAt: '',
        data: {},
        schoolclazzs: '',
        star: '',
        type: '',
        uid: '',
        updatedAt: '',
        schoolstudentid: '',
        schoolhomeworkid: '',
        schoolhomework: {
          schoolhomeworkitembank: []
        }
      },
      image: '',
      index: 0,
      context: '',
      ID: ''
    }
  },
  async onLoad (query) {
    this.ID = query.ID
    this.index = 0
    this.pj = true
    await this.start()
  },
  watch: {
    context (v) {
      console.log(v)
    }
  },
  computed: {
    itembank () {
      const { index, homework } = this
      return homework.schoolhomework.schoolhomeworkitembank[index] || {}
    },
    reply () {
      const { itembank } = this
      const data = itembank.schoolhomeworkitembankreply || []
      return sortBy(data, v => -v.ID)
    },
    schoolhomeworkitembank () {
      return this.homework.schoolhomework.schoolhomeworkitembank
    },
    data () {
      const { itembank } = this
      const { data } = itembank
      if (data) {
        return data.answer || ''
      }
      return ''
    },
    userdata () {
      const { itembank } = this
      const { schoolstudenthomeworkitembank, type } = itembank
      if (schoolstudenthomeworkitembank) {
        if (type === '单选' || type === '解答') {
          return schoolstudenthomeworkitembank[0].answer
        } else if (type === '多选') {
          const data = schoolstudenthomeworkitembank[0].answer.split(' ')
          const x = {}
          for (let i of data) {
            x[i] = i
          }
          return x
        }
        return schoolstudenthomeworkitembank[0].answer.split(' ')
      }
      return ''
    }
  },
  methods: {
    async start () {
      const { ID } = await getuser('user')
      try {
        let code = await wxlogin()
        const data = await request(getschoolstudenthomeworkbyid, {
          data: {
            schoolstudentid: ID,
            ID: this.ID,
            code
          }
        })
        if (!data.data) {
          data.data = {}
        }
        this.homework = data
      } catch (err) {
        wx.showModal({
          title: '提示',
          content: '服务器请求失败，点击确定重新加载',
          success: res => {
            if (res.confirm) {
              this.start()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },
    async submit (e) {
      const { ID } = await getuser('user')
      const { itembank, context } = this
      if (!context) return
      const schoolhomeworkitembankid = itembank.ID
      try {
        let code = await wxlogin()
        await request2(changeschoolhomeworkitembankreply, {
          data: {
            schoolstudentid: ID,
            schoolhomeworkitembankid,
            context,
            state: false,
            code,
            schooluserid: this.homework.uid
          }
        })
        this.context = ''
        this.visible2 = false
        await this.start()
      } catch (err) {
        wx.showModal({
          title: '提示',
          content: '服务器请求失败，点击确定重新加载',
          success: res => {
            if (res.confirm) {
              this.start()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },
    handleCancel1 () {
      this.visible1 = false
    },
    handleCancel2 (i) {
      console.log(i)
      this.index = i
      this.visible2 = true
    },
    change ({target}) {
      const { dataset, id, ...data } = target
      this.context = values(data).join('')
    },
    key (i, k) {
      return `${i}assignment${k}`
    }
  }
}
